// 获取mongoose
const mongoose = require("mongoose");
//get电影列表数据
const get = async ({
    curPage,
    eachPage
}) => {
    // 强制转换类型 number转string
    (curPage = ~~curPage), (eachPage = ~~eachPage);
    const data = {
        total: 0,
        maxPage: 0,
        rows: [],
    }

    // 是获取数据库电影的数据
    const moviesModel = mongoose.model("movies");
    // 获取当前model映射的那个集合里存在的文档总数
    data.total = await moviesModel.countDocuments();
    // Math.ceil往上取整
    data.maxPage = Math.ceil(data.total / eachPage);


    //查询数据库所有数据 跳过（当前页-1）*当前页显示数据  limit 查询多少条数据  sort 排序  execute 立即执行
    const skipNum = (curPage - 1) * eachPage;
    data.rows = await moviesModel.find().skip(skipNum).limit(eachPage).sort({
        _id: -1
    }).exec();
    return data;
};

// 删除影院
const deleteMovie = async ({
    _id
}) => {
    const moviesModel = mongoose.model("movies");
    // remove删除数据库_id匹配的数据
    await moviesModel.remove({
        _id
    });
};

// query查询条件   params需要修改的数据
const update = async (query, params) => {
    const moviesModel = mongoose.model("movies");
    console.log(query._id, params.$push);
    // 修改数据
    await moviesModel.updateOne(query, params);
    return moviesModel.findOne({
        _id: query._id
    });
}

const findOne = async _id => {
    // model传一个参数时表示获取这个model对象
    const moviesModel = mongoose.model("movies");
    // 添加电影数据
    return await moviesModel.findOne({
        _id
    });
}

//删除电影海报
const delImgs = async ({
    _id,
    url
}) => {
    const moviesModel = mongoose.model("movies");
    // findOne找到匹配的数据立即返回，不会接着匹配数据
    const movie = await moviesModel.findOne({
        _id
    });
    // 删除url对应的图片，并保存！！！！！sava
    movie.images.pull(url);
    await movie.save();
}


//删除电影海报
const delposter = async ({
    _id,
    url
}) => {
    const moviesModel = mongoose.model("movies");
    // findOne找到匹配的数据立即返回，不会接着匹配数据
    const movie = await moviesModel.findOne({
        _id
    });
    // 删除url对应的图片，并保存！！！！！sava
    movie.poseter.pull(url);
    await movie.save();
}

// 新增电影数据
const addMovie = async ({
    name,
    ename,
    type,
    area,
    time,
    upDate,
    score,
    count,
    intro,
    isClassic,
    director,
    actor
}) => {
    // console.log(name,ename,type,area,time,upDate,score,count,intro,isClassic,director,actor);
    const moviesModel = mongoose.model("movies");
    // 先通过用户输入的电影名字在数据库中查找，看是否存在

    const data =await moviesModel.find({
        name
    })

    if (data.length) {
        return false

    } else {
        
      return  await moviesModel.create({
            name,
            ename,
            type,
            area,
            time,
            upDate,
            score,
            count,
            intro,
            isClassic,
            director,
            actor
        });
       
    }
}
// 修改影院
const alertMovie = async (newMovie, _id) => {
    const moviesModel = mongoose.model("movies");
    //在movies集合修改数据
    await moviesModel.updateOne({
        _id: newMovie._id
    }, newMovie);
}
// 查找所有信息
const select = async (data) => {
    const moviesModel = mongoose.model("movies");
    return await moviesModel.find(data);
}

//暴露接口
module.exports = {
    select,
    get,
    addMovie,
    deleteMovie,
    alertMovie,
    findOne,
    update,
    delImgs,
    delposter
} //add, getOne, update,delImg